package com.klun.concDesign.chapterFour;

import sun.misc.Unsafe;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;


public class ThreadLocalDemo_Gc {
    static volatile ThreadLocal t1 = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected void finalize() throws Throwable {
            System.out.println(this.toString() + "is gc");
            super.finalize();
        }
    };
    static volatile CountDownLatch cd  = new CountDownLatch(10000);
    public static class ParseDate implements Runnable{
        int i = 0;
        public ParseDate(int i){
            this.i = i;
        }
        @Override
        public void run() {
            try{
                // 如果当前线程的 ThreadLocalMao 为null
                if(t1.get() == null){
                    t1.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"){
                        @Override
                        protected void finalize() throws Throwable {
                            System.out.println(this.toString() + "is GC");
                            super.finalize();
                        }
                    });
                    System.out.println(Thread.currentThread().getId() + ":create SimpleDateFormat");
                }
                Object o = t1.get();
                SimpleDateFormat o1 = (SimpleDateFormat) o;
                Date t = o1.parse("2015-03029 19:29:" + i % 60);
            } catch (Exception e){
                e.printStackTrace();
            } finally {
                cd.countDown();
            }

        }
    }

    public static void main(String[] args) {
        // Unsafe 为 JDK 的内部专享类，不能调用
        Unsafe unsafe = Unsafe.getUnsafe();
        // AtomicReference
        // AtomicInteger


    }

}
